package com.woniu.zsd.platform.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.zsd.platform.dto.RoleDto;
import com.woniu.zsd.platform.model.Role;
import com.woniu.zsd.platform.dao.RoleMapper;
import com.woniu.zsd.platform.service.RoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhangtao
 * @since 2025年02月18日
 */
@Service("plateformRoleService")
@Primary
public class RoleServiceImpl extends ServiceImpl<RoleMapper, Role> implements RoleService {
    @Resource
    private RoleMapper roleMapper;

    @Override
    public List<RoleDto> selectAllRole() throws Exception {
        List<Role> roles = roleMapper.selectList(null);
        List<RoleDto> roleDtos = BeanUtil.copyToList(roles, RoleDto.class);
        return roleDtos;
    }

    @Override
    public void addRole(String roleName) throws Exception {
        Role role = roleMapper.selectOne(new QueryWrapper<Role>().eq("role_name", roleName));
        if(role != null){
            throw new Exception("角色名称已存在");
        }
        Role role1 = new Role();
        role1.setRoleName(roleName);
        role1.setRoleStatus(1);
        roleMapper.insert(role1);
    }

    @Override
    public void deleteRole(Integer roleId) throws Exception {
        Role role = roleMapper.selectById(roleId);
        if(role == null){
            throw new Exception("角色不存在");
        }
        roleMapper.deleteById(roleId);
    }
}
